<?php

//公共内容
class PublicController extends Controller
{
    //验证码
    public function verifyAction()
    {
        session_start();
        verify::buildImageVerify(6, 1, 'png', 70, 30);
    }

    // 上传文件
    public function uploadimageAction()
    {
        $upload = new upload();
        if (!empty($_FILES)) {
            $file = array_shift($_FILES);
            $hash = md5(F($file['tmp_name']));
            $url = M('attachment')->where(array('hash' => $hash))->getfield('url');
            if ($url) $this->ajax(array("success" => true, 'msg' => 'success', 'file_path' => $url,));
            C('storage_path', 'uploads');
            if (!empty($_POST['original_filename'])) $file['name'] = $_POST['original_filename'];
            $upload->setFile($file); //文件源
            $data = $upload->uploadone();
            if ($data['status'] == 1) {
                //添加到附件表
                if (in_array($data['info']['ext'],array('jpg','png','gif','bmp','jpeg'))){
                    //如果是图片 则增加水印
                    $image = new image(url2path($data['info']['fileurl']));
                    $image->water(C('water_file'));
                    storage::write($data['info']['filepath'], $image->save());
                }
                $param = array(
                    'user_id' => intval(cookie('userid')),
                    'name' => $data['info']['filename'],
                    'path' => $data['info']['filepath'],
                    'url' => $data['info']['fileurl'],
                    'ext' => $data['info']['ext'],
                    'size' => $data['info']['size'],
                    'hash' => $data['info']['hash'],
                );
                M('attachment')->add($param);
                $ajax = array("success" => true, 'msg' => 'success', 'file_path' => $data['info']['fileurl'],);
            } else {
                $ajax = array("success" => false, 'msg' => $data['info']);
            }
        } else {
            $ajax = array("success" => false, 'msg' => '没有找到上传的文件');
        }
        $this->ajax($ajax);
    }

    public function uploadfileAction()
    {
        $upload = new upload();
        if (!empty($_FILES)) {
            $file = array_shift($_FILES);
            $hash = md5(F($file['tmp_name']));
            $id = M('attachment')->where(array('hash' => $hash))->getfield('id');
            if ($id) $this->ajax(array("success" => true, 'msg' => 'success', 'file_path' => U('common.public.attachment',array('id'=>$id)),));
            C('storage_path', 'uploads');
            $upload->setFile($file); //文件源
            $data = $upload->uploadone();
            if ($data['status'] == 1) {
                $param = array(
                    'user_id' => intval(cookie('userid')),
                    'name' => $data['info']['filename'],
                    'path' => $data['info']['filepath'],
                    'url' => $data['info']['fileurl'],
                    'ext' => $data['info']['ext'],
                    'size' => $data['info']['size'],
                    'hash' => $data['info']['hash'],
                );
                $id=M('attachment')->add($param);
                if ($id){
                    $ajax = array("success" => true, 'msg' => 'success', 'file_path' => U('common.public.attachment',array('id'=>$id)));
                }else{
                    $ajax = array("success" => true, 'msg' => 'success', 'file_path' => $data['info']['fileurl']);
                }
            } else {
                $ajax = array("success" => false, 'msg' => $data['info']);
            }
        } else {
            $ajax = array("success" => false, 'msg' => '没有找到上传的文件');
        }
        $this->ajax($ajax);
    }
}

